package com.bargate.domain.spring.expression.value;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * Created by IntelliJ IDEA.
 * User: EAD-MASTER
 * Date: 02/10/2010
 * Time: 12:11:06
 * To change this template use File | Settings | File Templates.
 * <p/>
 * <p/>
 * <p/>
 * /**
 *
 * @see org.springframework.context.expression.StandardBeanExpressionResolver
 */
public class ExpressionTemplatingOperations {
    private EvaluationContext context = new StandardEvaluationContext();
    private ExpressionParser parser = new SpelExpressionParser();

    public void testExpressionTemplating() {
        String fooBarBaz = parser.parseExpression("foo ??'foobar'.substring(3)? ??'barbaz'.substring(3)?",
                new MyParserContext()).getValue(String.class);
        System.out.println(fooBarBaz);
    }

    public void testExpressionTemplating2() {
        Double result = parser.parseExpression("15 * ?[HurricaneKatrina.maxWindVelocity]", new MyParserContext2()).getValue(Double.class);
        System.out.println("result = " + result);
    }

    public static void main(String[] args) {
        ExpressionTemplatingOperations expressionTemplatingOperations = new ExpressionTemplatingOperations();
        expressionTemplatingOperations.testExpressionTemplating();
        expressionTemplatingOperations.testExpressionTemplating2();
    }

    private static class MyParserContext implements ParserContext {
        public String getExpressionPrefix() {
            return "??";
        }

        public String getExpressionSuffix() {
            return "?";
        }

        public boolean isTemplate() {
            return true;
        }
    }

    private static class MyParserContext2 implements ParserContext {
        public boolean isTemplate() {
            return true;
        }

        public String getExpressionPrefix() {
            return "?[";
        }

        public String getExpressionSuffix() {
            return "]";
        }
    }
}
